package com.example.teaching_server.controller;

import com.example.teaching_server.common.ApiResponse;
import com.example.teaching_server.dto.StudentDTO;
import com.example.teaching_server.entity.TermEntity;
import com.example.teaching_server.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/students")
@RequiredArgsConstructor
@Tag(name = "学生管理", description = "学生信息查询接口")
public class StudentController {

    private final StudentService studentService;

    /**
     * 获取所有学期
     */
    @GetMapping("/terms")
    @Operation(summary = "获取学期列表", description = "获取所有可用的学期列表")
    public ApiResponse<List<TermEntity>> getAllTerms() {
        log.info("开始获取学期列表");

        try {
            List<TermEntity> terms = studentService.getAllTerms();
            log.info("成功获取{}个学期", terms.size());
            return ApiResponse.success("学期列表获取成功", terms);
        } catch (Exception e) {
            log.error("获取学期列表失败", e);
            return ApiResponse.fail("获取学期列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有年级
     */
    @GetMapping("/grades")
    @Operation(summary = "获取年级列表", description = "获取所有可用的年级列表")
    public ApiResponse<List<String>> getAllGrades() {
        log.info("开始获取年级列表");

        try {
            List<String> grades = studentService.getAllGradeLevels();
            log.info("成功获取{}个年级", grades.size());
            return ApiResponse.success("年级列表获取成功", grades);
        } catch (Exception e) {
            log.error("获取年级列表失败", e);
            return ApiResponse.fail("获取年级列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据条件查询学生
     */
    @GetMapping
    @Operation(summary = "查询学生列表", description = "根据教师ID、学期ID和年级级别查询学生列表")
    public ApiResponse<List<StudentDTO>> getStudentsByCriteria(
            @Parameter(description = "教师ID") @RequestParam Long teacherId,
            @Parameter(description = "学期ID") @RequestParam(required = false) Long termId,
            @Parameter(description = "年级级别") @RequestParam(required = false) String gradeLevel) {

        log.info("开始查询学生列表: teacherId={}, termId={}, gradeLevel={}", teacherId, termId, gradeLevel);

        try {
            List<StudentDTO> students = studentService.getStudentsByCriteria(teacherId, termId, gradeLevel);
            log.info("成功查询到{}个学生", students.size());
            return ApiResponse.success("学生列表获取成功", students);
        } catch (Exception e) {
            log.error("查询学生列表失败: teacherId={}, termId={}, gradeLevel={}", teacherId, termId, gradeLevel, e);
            return ApiResponse.fail("获取学生列表失败: " + e.getMessage());
        }
    }
}
